/**
 * 给你两个非空的链表，表示两个非负的整数。
 * 它们每位数字都是按照逆序的方式存储的，
 * 并且每个节点只能存储一位数字。请你将两数相加，
 * 并以相同形式返回一个表示和的链表
 * 你可以假设除了数字0之外，这两个数都不会以0开头
 * 输入: 2 -> 4 -> 3
 *       5 -> 6 -> 4
 * 输出: 7 -> 0 -> 8
*/

function Node (val) {
    this.val = val;
    this.next = null;
}

function addTwoNumbers (l1, l2) {
    const result = new Node(0);
    let curr = result;
    let next1 = 0;
    // 先遍历两个链表
    while (l1 && l2) {
        const total = l1.val + l2.val + next1;
        next1 = parseInt(total / 10);
        curr.next = new Node(total % 10);
        curr = curr.next;
        l1 = l1.next;
        l2 = l2.next;
    }
    // 找到链表1剩余数，遍历完成
    if (l1) {
        const total = l1.val + next1;
        next1 = parseInt(total / 10);
        curr.next = new Node(total % 10);
        curr = curr.next;
        l1 = l1.next;
    }
    // 找到链表2剩余数，遍历完成
    if (l2) {
        const total = l2.val + next1;
        next1 = parseInt(total / 10);
        curr.next = new Node(total % 10);
        curr = curr.next;
        l2 = l2.next;
    }
    // 看看有没有剩余的进位数加上就行了
    if (next1) {
        curr.next = new Node(next1);
    }
    return result.next;
}

const nodeL1_2 = new Node(9);
const nodeL1_4 = new Node(9);
const nodeL1_3 = new Node(9);
nodeL1_2.next = nodeL1_4;
nodeL1_4.next = nodeL1_3;

const nodeL2_5 = new Node(9);
const nodeL2_6 = new Node(9);
nodeL2_5.next = nodeL2_6;



console.log(addTwoNumbers(nodeL1_2, nodeL2_5));